<?php

namespace app\zbj\controller;

use think\Controller;
use think\Db;
use think\Request;

class Agents extends Base
{
    // 代理商列表
    public function index(Request $request)
    {

        $get=myarr($request->get());
        $get['p']=isset($get['p'])?$get['p']:config('paginate.list_rows');
        $list = Db::name('users')->alias('a')
            ->join('lc_agents b','a.aid=b.id')
            ->field('b.id,b.name,count(a.id) c')->where('a.refereecode',0)->group('a.aid')->order('id desc')->paginate();
        $page=$list->render();
        $list=$list->items();
        return view('agents/index',['list'=>$list,'page'=>$page,'get'=>$get]);
    }



    public function showCustomer(Request $request)
    {

        $id=$request->get('id');
        $name=Db::name('users')->where('id',$id)->find()['name'];
        $list = Db::name('users')->where(['aid'=>$id])
            ->where('refereecode',0)
            ->paginate(15,false,['query'=>$_GET]);
        $page=$list->render();
        $list=$list->items();
        foreach ($list as $k=>$v){
            if(strlen($v['name'])==11){
                $list[$k]['name']=substr_replace($v['name'], '*****', 3, 5);
            }else{
                $list[$k]['name']=substr_replace($v['name'], '*', 3, 3);
            }
            $list[$k]['phonenumber']=substr_replace($v['phonenumber'], '*****', 3, 5);
        }

        return view('agents/showCustomer',['list'=>$list,'name'=>$name,'page'=>$page]);

    }

    public function show(Request $request)
    {
        $id=$request->get('id');
//        $user=Db::name('users')->field('name,assetaccount')->find($id);
        $user=Db::name('users')->field('name,assetaccount,fwq')->find($id);
        $qjgj = Db::name('futures_api')->where(['id'=>$user['fwq']])->find();
//        $qjgj = Db::name('futures_api')->where(['state'=>1])->find();
        if($qjgj['title']=='jt'){
            $req['Action']='ReqQryAccountInfo';
            $req['ChdAccountID']=$user['assetaccount'];
            $req['UserID']=$qjgj['sa'];
            ksort($req);
            $url='';
            foreach ($req as $k=>$v){
                $url=$url.$k.'='.$v.'&';
            }
            $req=rtrim($url,'&');
            //var_dump($req);
            $sign=md5($req.'&UserKey='.$qjgj['sapass']);
            $req=$req.'&Sign='.$sign;
            //var_dump($req);
            $url=$qjgj['api'].'?'.$req;
            // var_dump($url);
            $res=https_request($url);
            //var_dump($res);die;
            //var_dump(json_decode($res,true));die;
            $data=json_decode($res,true);
            if(strlen($user['name'])==11){
                $user['name']=substr_replace($user['name'], '*****', 3, 5);
            }else{
                $user['name']=substr_replace($user['name'], '*', 3, 3);
            }
            $da = [];
            if($data['ErrorID']==0) {
                $da['dqqy'] = $data['LowAvailable']/100;//劣后
                $da['Credit'] = $data['HighBalance']/100;//优先
                $da['Margin']=$data['CurrMargin'];//保证金
                $da['Balance'] = $data['Balance']/100;//当前权益
                $da['Available'] = $data['Available']/100;//可用资金
//                $data['']=$data['BadAmount'];
                return ['status'=>1,'res'=>$da,'name'=>$user['name']];
            }else{
                return ['status'=>2,'msg'=>$data['ErrorMsg'],'name'=>$user['name']];
            }

        }else{
            $url="{$qjgj['api']}queryaccount?requestid=6&sa={$qjgj['sa']}&sapass={$qjgj['sapass']}&account={$user['assetaccount']}";
            $data=https_request($url);
            $objectxml = simplexml_load_string($data);//将文件转换成 对象
            $xmljson= json_encode($objectxml );//将对象转换个JSON
            $xmlarray=json_decode($xmljson,true);//将json转换成数组
            if(strlen($user['name'])==11){
                $res['name']=substr_replace($user['name'], '*****', 3, 5);
            }else{
                $res['name']=substr_replace($user['name'], '*', 3, 3);
            }
            if($xmlarray['Result']['Error']['Code']!=0){
                return ['status'=>2,'msg'=>$xmlarray['Result']['Error']['Message'],'name'=>$user['name']];
            }else{
                return ['status'=>1,'res'=>$xmlarray['Result']['Summary'],'name'=>$user['name']];
            }
        }
        return $res;


    }
}
